<template>
  <div>
    <a-drawer
      v-model:open="props.open"
      title="批量删除"
      placement="right"
      @close="onClose"
      size="large"
    >
      <div>
        <span style="font-size: 16px; color: #020817">删除信息</span>
      </div>
      <a-divider />
      <a-form>
        <a-form-item label="项目名称" name="regionName">
          <a-input v-model:value="formData.regionName" />
        </a-form-item>
        <a-form-item label="操作类型" name="operationType">
          <a-select v-model:value="formData.operationType">
            <a-select-option value="1">删除</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="操作对象" name="operateObjectName">
          <a-input
            v-model:value="formData.operateObjectName"
            placeholder="请输入操作对象"
          />
        </a-form-item>
      </a-form>
      <div>
        <span style="font-size: 16px; color: #020817">申请信息</span>
      </div>
      <a-divider />
      <a-form>
        <a-form-item label="申请原因" name="applicationReason">
          <a-textarea
            v-model:value="formData.applicationReason"
            placeholder="请输入申请原因"
            :rows="4"
          />
        </a-form-item>
        <a-form-item label="附件" name="name">
          <a-upload
            v-model="fileList"
            action="/api/ChargeSystem/upload/attachmentUpload"
            :headers="header"
            :beforeUpload="beforeUploadAttachment"
            accept=".pdf,.doc,.docx,.xls,.xlsx,.png,.jpg"
            @change="handleChanges"
          >
            <a-button>
              <upload-outlined></upload-outlined>
              上传
            </a-button>
          </a-upload>
        </a-form-item>
      </a-form>
      <template #footer>
        <a-space>
          <a-button type="primary" @click="submitForm">提交</a-button>
          <a-button @click="onClose">取消</a-button>
        </a-space>
      </template>
    </a-drawer>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive, watch } from 'vue';
import { UploadOutlined } from '@ant-design/icons-vue';
// 注意：请根据实际项目结构调整API导入路径
import {
  getHeatExchangeStationBatchOperation,
  getHeatExchangeStationBatchOperation_two,
  getVillageBatchOperation,
  getBuildingBatchOperation,
  getUnitBatchOperation,
  getRoomBatchOperation,
  getHeatSourceBatchOperation,
  getHeatExchangeStationBatchOperation_three,
  delVillageBatchOperation,
  delBuildingBatchOperation,
  delUnitBatchOperation,
  delRoomBatchOperation,
} from '#/api/core/customerInformation';
import { useAccessStore } from '@vben/stores';
import { message } from 'ant-design-vue';
const props = defineProps({
  open: {
    type: Boolean,
    default: false,
  },
  selectedType: {
    type: String,
    default: '热源', // 默认值可以根据需要设置
  },
  selectedNodeId: {
    type: Array,
    default: [],
  },
  selectedLevel: {
    type: Number,
    default: 0,
  },
});
const accessStore = useAccessStore();
const header = {
  Authorization: accessStore.accessToken || localStorage.getItem('token'),
};
const formData = reactive({
  regionId: '',
  regionName: '',
  operationType: '1',
  operationObject: '',
  operateObjectName: '',
  applicationReason: '',
  attachmentLocation: '',
});
// 定义响应式变量
const value = ref('');
const fileList = ref([]);
// 监听弹窗打开状态，当弹窗打开时执行getObject
watch(
  () => props.open,
  (newVal) => {
    if (newVal) {
      getObject();
    }
  },
);
const emit = defineEmits(['onClose']);
const onClose = () => {
  emit('onClose');
};
const getObject = () => {
  // TODO: 请根据实际项目需要恢复API调用
  switch (props.selectedType) {
    case '热源':
      getHeatExchangeStationBatchOperation(props.selectedNodeId.join(',')).then(
        (res: any) => {
          console.log(res);
          formData.regionName = res.data.data.comNameLevel5;
          formData.regionId = res.data.data.regionId;
          formData.operateObjectName = res.data.data.operateObjectName;
          formData.operationObject = res.data.data.operateObject;
        },
      );
      break;
    case '热站':
      getHeatExchangeStationBatchOperation_two(
        props.selectedNodeId.join(','),
      ).then((res: any) => {
        console.log(res);
        formData.regionName = res.data.data.comNameLevel5;
        formData.regionId = res.data.data.regionId;
        formData.operateObjectName = res.data.data.operateObjectName;
        formData.operationObject = res.data.data.operateObject;
      });
      break;
    case '小区':
      getVillageBatchOperation(props.selectedNodeId.join(',')).then(
        (res: any) => {
          console.log(res);
          formData.regionName = res.data.data.comNameLevel5;
          formData.regionId = res.data.data.regionId;
          formData.operateObjectName = res.data.data.operateObjectName;
          formData.operationObject = res.data.data.operateObject;
        },
      );
      break;
    case '热用户':
      if (props.selectedLevel === 9) {
        getBuildingBatchOperation(props.selectedNodeId.join(',')).then(
          (res: any) => {
            console.log(res);
            formData.regionName = res.data.data.comNameLevel5;
            formData.regionId = res.data.data.regionId;
            formData.operateObjectName = res.data.data.operateObjectName;
            formData.operationObject = res.data.data.operateObject;
          },
        );
      } else if (props.selectedLevel === 10) {
        getUnitBatchOperation(props.selectedNodeId.join(',')).then(
          (res: any) => {
            console.log(res);
            formData.regionName = res.data.data.comNameLevel5;
            formData.regionId = res.data.data.regionId;
            formData.operateObjectName = res.data.data.operateObjectName;
            formData.operationObject = res.data.data.operateObject;
          },
        );
      } else if (props.selectedLevel === 11) {
        getRoomBatchOperation(props.selectedNodeId.join(',')).then(
          (res: any) => {
            console.log(res);
            formData.regionName = res.data.data.comNameLevel5;
            formData.regionId = res.data.data.regionId;
            formData.operateObjectName = res.data.data.operateObjectName;
            formData.operationObject = res.data.data.operateObject;
          },
        );
      }
  }
};
const attachmentLocation = ref('');
const handleChanges = (info: any) => {
  console.log(info);
  if (info.file.status === 'done') {
    if (info.file.response && info.file.response.code === 0) {
      message.success(`${info.file.name} 文件上传成功`);
      attachmentLocation.value = info.file.response.data.fileLocation;
    } else {
      message.error(
        `${info.file.name} 文件上传失败: ${info.file.response ? info.file.response.message : '未知错误'}`,
      );
    }
  } else if (info.file.status === 'error') {
    message.error(`${info.file.name} 文件上传失败.`);
  }
};

const beforeUploadAttachment = (file: File) => {
  const isPdf = file.type === 'application/pdf';
  const isDoc = file.type === 'application/msword';
  const isDocx =
    file.type ===
    'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
  const isXls = file.type === 'application/vnd.ms-excel';
  const isXlsx =
    file.type ===
    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  const isPng = file.type === 'image/png';
  const isJpg = file.type === 'image/jpeg';
  const isLt2M = file.size / 1024 / 1024 < 2;

  const isSupportedFormat =
    isPdf || isDoc || isDocx || isXls || isXlsx || isPng || isJpg;

  if (!isSupportedFormat) {
    message.error('仅支持pdf/doc/docx/xls/xlsx/png/jpg格式文件!');
  }
  if (!isLt2M) {
    message.error('文件大小限制2MB以内!');
  }
  return isSupportedFormat && isLt2M;
};
const submitForm = () => {
  const data = {
    regionId: formData.regionId,
    operateObject: formData.operationObject,
    applicationReason: formData.applicationReason,
    attachmentLocation: attachmentLocation.value,
  };
  console.log(formData);
  switch (props.selectedType) {
    case '热源':
      getHeatSourceBatchOperation(data).then((res: any) => {
        console.log(res);
        if (res.data.code == 200) {
          message.success('删除成功');
          onClose();
        } else {
          message.error('删除失败');
        }
      });
      break;
    case '热站':
      getHeatExchangeStationBatchOperation_three(data).then((res: any) => {
        console.log(res);
        if (res.data.code == 200) {
          message.success('删除成功');
          onClose();
        } else {
          message.error('删除失败');
        }
      });
      break;
    case '小区':
      delVillageBatchOperation(data).then((res: any) => {
        console.log(res);
        if (res.data.code == 200) {
          message.success('删除成功');
          onClose();
        } else {
          message.error('删除失败');
        }
      });
      break;
    case '热用户':
      if (props.selectedLevel === 9) {
        delBuildingBatchOperation(data).then((res: any) => {
          console.log(res);
          if (res.data.code == 200) {
            message.success('删除成功');
            onClose();
          } else {
            message.error('删除失败');
          }
        });
      } else if (props.selectedLevel === 10) {
        delUnitBatchOperation(data).then((res: any) => {
          console.log(res);
          if (res.data.code == 200) {
            message.success('删除成功');
            onClose();
          } else {
            message.error('删除失败');
          }
        });
      } else if (props.selectedLevel === 11) {
        delRoomBatchOperation(data).then((res: any) => {
          console.log(res);
          if (res.data.code == 200) {
            message.success('删除成功');
            onClose();
          } else {
            message.error('删除失败');
          }
        });
      }
      break;
  }
};
</script>
<style scoped></style>
